import { getPayListApi } from './api'
/**
 * @param {number} time
 * @param {string} cFormat
 */
let payList = []
export function formatSeconds (time, cFormat) {
  const format = cFormat || '{h}:{i}:{s}'
  const result = parseInt(time)
  const formatObj = {
    h: Math.floor(result / 3600),
    i: Math.floor((result / 60 % 60)),
    s: Math.floor((result % 60))
  }

  return format.replace(/{([his])+}/g, (result, key) => {
    const value = formatObj[key]
    return value.toString().padStart(2, '0')
  })
}
export function getPayList () {
  return new Promise((resolve, reject) => {
    if (payList.length) {
      resolve(payList)
    } else {
      getPayListApi().then(res => {
        const data = res.data || {}
        const list = data.list || []
        payList = list
        resolve(list)
      }).catch(error => {
        reject(error)
      })
    }
  })
}
